﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace BLL
{
  public class RoleBLL
  {
    public RoleBLL(string serverPath)
    {
      path = serverPath;
    }
    private string path = "";
    public struct Role
    {
      public string Id;
      public string Name;
      public Role(string id, string name)
      {
        Id = id;
        Name = name;
      }
    }
    public List<Role> GetAll()
    {
      XElement xElement = XElement.Load(path + "XML/Config.xml");
      var xRole = xElement.Descendants("Roles").Descendants("Role");
      List<Role> list = new List<Role>();
      foreach (XElement item in xRole)
      {
        Role role = new Role(item.Attribute("Id").Value, item.Element("Name").Value);
        list.Add(role);
      }
      return list;
    }

    public Role GetById(int id)
    {
      XElement xElement = XElement.Load(path + "XML/Config.xml");
      var xRole = xElement.Descendants("Roles").Descendants("Role").Where(p => p.Attribute("Id").Value == id.ToString());
      XElement role = xRole.FirstOrDefault();

      return new Role(role.Attribute("Id").Value, role.Element("Name").Value);
    }
  }
}
